package serive;

import Dao.UserDao;
import Dao.UserDaoImpl;

public class UserServiceImpl implements UserService {
    UserDao Udao = new UserDaoImpl();

    @Override
    public boolean login(String id, String pwd) {
        String rPsw = Udao.login(id);
        if (rPsw != null && rPsw.equals(pwd)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean register(String id, String psw1, String psw2, String Email) {
        //用户名:
        //必须是6-10位字母、数字、下划线(这里字母、数字、下划线是指任意组合，没有必须三类均包含)
        //不能以数字开头
        String idRegex = "^[^0-9][\\w_]{5,9}$";

        //密码:
        //必须是6-20位的字母、数字、下划线(这里字母、 数字下划线是指任意组合,没有必须三类均包含)
        String pswRegex ="^[\\w_]{6,20}$";
        boolean matchesId = id.matches(idRegex);
        boolean matchesPsw = psw1.matches(pswRegex);

        if (psw1.equals(psw2)) {
            if (matchesId&&matchesPsw){
                Boolean b = Udao.register(id, psw1, Email);
                if (b) {
                    return true;
                } else {
                    return false;
                }
            }
        }
        return false;
    }
}
